package jinyilw.swing.visuals.Rcomp;

import jinyilw.common.Operator;
import jinyilw.swing.ScreenTools;
import jinyilw.swing.i18n.ICSetting;
import jinyilw.swing.icon.LazyScaleIcon;
import jinyilw.swing.rlaf.UITools;
import jinyilw.swing.rlaf.ui.RRootPaneUI;
import jinyilw.swing.rlaf.ui.RTitlePane;
import jinyilw.swing.visuals.comp.ScaleImageComp;

import javax.swing.JDialog;
import javax.swing.JLayeredPane;
import javax.swing.plaf.RootPaneUI;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Image;
import java.awt.Window;

public class RDialog extends JDialog
{
	private static final long serialVersionUID = -3604680178901370659L;
	private Dimension maxDimension;
	private Dimension defaultDimension;

	private RRootPaneUI rRootPaneUI = null;
	private RTitlePane rTitlePane = null;

	public RDialog()
	{
		initGUI();
	}

	public RDialog(Frame owner)
	{
		super(owner);
		initGUI();
	}

	public RDialog(Dialog owner)
	{
		super(owner);
		initGUI();
	}

	public RDialog(Window owner)
	{
		super(owner);
		initGUI();
	}

	public RDialog(Frame owner, boolean modal)
	{
		super(owner, modal);
		initGUI();
	}

	public RDialog(Frame owner, String title)
	{
		super(owner, title);
		initGUI();
	}

	public RDialog(Dialog owner, boolean modal)
	{
		super(owner, modal);
		initGUI();
	}

	public RDialog(Dialog owner, String title)
	{
		super(owner, title);
		initGUI();
	}

	public RDialog(Window owner, ModalityType modalityType)
	{
		super(owner, modalityType);
		initGUI();
	}

	public RDialog(Window owner, String title)
	{
		super(owner, title);
		initGUI();
	}

	public RDialog(Frame owner, String title, boolean modal)
	{
		super(owner, title, modal);
		initGUI();
	}

	public RDialog(Dialog owner, String title, boolean modal)
	{
		super(owner, title, modal);
		initGUI();
	}

	public RDialog(Window owner, String title, ModalityType modalityType)
	{
		super(owner, title, modalityType);
		initGUI();
	}

	public RDialog(Frame owner, String title, boolean modal,
			GraphicsConfiguration gc)
	{
		super(owner, title, modal, gc);
		initGUI();
	}

	public RDialog(Dialog owner, String title, boolean modal,
			GraphicsConfiguration gc)
	{
		super(owner, title, modal, gc);
		initGUI();
	}

	public RDialog(Window owner, String title, ModalityType modalityType,
			GraphicsConfiguration gc)
	{
		super(owner, title, modalityType, gc);
		initGUI();
	}

	private void initGUI()
	{
		RootPaneUI ui = getRootPane().getUI();
		if (ui instanceof RRootPaneUI)
		{
			rRootPaneUI = (RRootPaneUI) ui;
			rTitlePane = rRootPaneUI.getTitlePane();
		}
		maxDimension = ScreenTools.getDefaultScreenSize();
		int w = (int) (maxDimension.width * 0.6);
		int h = (int) (maxDimension.height * 0.6);
		defaultDimension = new Dimension(w, h);
		setSection(1);
	}

	public void setBGOpacity(int opacity)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setBrightness(opacity);
	}

	public void setCustomBGImage(Image img)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setCustomImage(img);
	}

	public void setTitleColor(Color color)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleColor(color);
	}

	public void setTitleHeight(int height)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleHeight(height);
	}

	public void setTitleFont(Font font)
	{
		if (rTitlePane != null)
			rTitlePane.setTitleFont(font);
	}

	public void setTitle(ICSetting iSetting)
	{
		if (rTitlePane != null)
			rTitlePane.setTitle(iSetting);
	}

	public void setTitle(ICSetting iSetting, boolean zooOutEn)
	{
		if (rTitlePane != null)
			rTitlePane.setTitle(iSetting, zooOutEn);
	}

	public void addClosingOperator(Operator<?> operator)
	{
		if (rTitlePane != null)
			rTitlePane.addClosingOperator(operator);
	}

	public void setWindowSymbol(Component com)
	{
		if (rTitlePane != null)
			rTitlePane.setSymbol(com);
	}

	public void setWindowSymbol(Class<?> loadClass, String path)
	{
		setWindowSymbol(LazyScaleIcon.getIcon(loadClass, path));
	}

	public void setWindowSymbol(LazyScaleIcon scaleIcon)
	{
		setWindowSymbol(new ScaleImageComp(scaleIcon));
	}

	public void addActionComponent(Component comp)
	{
		if (rTitlePane != null)
			rTitlePane.addActionComponent(comp);
	}

	public void setTransBrightStyle(boolean b)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setTransBrightStyle(b);
	}

	public void setOptionStyle(int br, float al)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setOptionStyle(br, al);
	}

	public void setSection(int n)
	{
		if (rRootPaneUI != null)
			rRootPaneUI.setContentSection(n);
	}

	public int getTitleHeight()
	{
		if (rTitlePane != null)
			return rTitlePane.getTitleHeight();
		return 0;
	}

	public int getSection()
	{
		if (rRootPaneUI != null)
			return rRootPaneUI.getContentSection();
		return 0;
	}

	public void setMaxDimension(Dimension d)
	{
		maxDimension = d;
	}

	public void setDefaultDimension(Dimension d)
	{
		defaultDimension = d;
	}

	public Dimension getMaxDimension()
	{
		return maxDimension;
	}

	public Dimension getDefaultDimension()
	{
		return defaultDimension;
	}

	public void setDefaultDimensionRatio(double wr, double hr)
	{
		int w = (int) (maxDimension.width * wr);
		int h = (int) (maxDimension.height * hr);
		defaultDimension = new Dimension(w, h);
	}

	public void defaultSize()
	{
		setSize(defaultDimension);
	}

	public void maxSize()
	{
		setSize(maxDimension);
	}

	public void makeCustom()
	{
		UITools.uninstallTheme(this);
	}

	public void addToLayeredPane(Component c)
	{
		getRootPane().getLayeredPane().add(c, JLayeredPane.FRAME_CONTENT_LAYER);
	}
}
